package params

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)

func init() {
	r := gin.Default()
	// rest api
	r.GET("/user/:name/*action", func(context *gin.Context) {
		name := context.Param("name")
		action := context.Param("action")
		action = strings.Trim(action, "/")
		context.String(http.StatusOK, name+" is "+action)
	})
	// url参数
	r.GET("/user", func(context *gin.Context) {
		name := context.DefaultQuery("name", "枯藤")
		context.String(http.StatusOK, fmt.Sprintf("hello %s", name))
	})
	r.POST("/form", func(context *gin.Context) {
		types := context.DefaultPostForm("type", "post")
		username := context.PostForm("username")
		password := context.PostForm("userpassword")
		context.String(http.StatusOK, fmt.Sprintf("username:%s,password:%s,type:%s", username, password, types))
	})
	err := r.Run(":8000")
	if err != nil {
		return
	}
}
